package com.ls.lserp.modules.material.service;

import java.math.BigDecimal;
import java.sql.SQLException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ls.lserp.common.persistence.Page;
import com.ls.lserp.common.service.CrudService;
import com.ls.lserp.common.utils.CollectionsUtils;
import com.ls.lserp.common.utils.DateUtils;
import com.ls.lserp.common.utils.ResourcesUtil;
import com.ls.lserp.dbaccess.Dao.OgnizationPEntityMapper;
import com.ls.lserp.dbaccess.Entity.OgnizationPEntity;
import com.ls.lserp.modules.common.entity.Material;
import com.ls.lserp.modules.common.entity.UploadFile;
import com.ls.lserp.modules.material.dao.AMaterialDao;
import com.ls.lserp.modules.material.entity.InventoryRequest;
import com.ls.lserp.modules.material.entity.InventoryRequestEx;
import com.ls.lserp.modules.order.entity.BarCode;
import com.ls.lserp.modules.setting.dao.InventoryWarningDao;
import com.ls.lserp.modules.setting.entity.InventoryWarning;
import com.ls.lserp.modules.setting.entity.Ognization;
import com.ls.lserp.modules.sys.dao.UploadFileDao;
import com.ls.lserp.modules.sys.entity.Brand;
import com.ls.lserp.modules.sys.utils.DesignoptionUtils;
import com.ls.lserp.modules.sys.utils.DictUtils;
import com.ls.lserp.modules.sys.utils.SequenceUtil;
import com.ls.lserp.modules.sys.utils.UserUtils;



/**
  * @ClassName: materialService
  * @Description: 物料 保存 修改 删除
  * 
  * @author 
  * @date 2017年1月19日 上午9:55:34
  *
  */

@Service
@Transactional(readOnly = false)
public class AMaterialService extends CrudService<AMaterialDao, Material> {
	@Autowired
	// 我的物理方法接口
	private AMaterialDao materialDao;
	//库存预警
	@Autowired
    private InventoryWarningDao  inventoryWarningDao;
	
	@Autowired
	private UploadFileDao  uploadFileDao;
	// 组织
	@Autowired
	private OgnizationPEntityMapper ognizationPEntityMapper;
	
    /**
      * @Title: findList
      * @Description: 查询我的物料
      * @param @param page
      * @param @param material
      * @param @return
      * @return Page<Material> 
      * @throws
      */
    
    public Page<Material> findList(Page<Material> page, Material material) {
        // 设置分页参数
    	material.setPage(page);
        // 执行分页查询
        page.setList(materialDao.findList(material));
        
        return page;
    }
    
	/**
	  * @Title: getMaterial
	  * @Description: 获得我的物料信息
	  * @param @param id
	  * @param @return
	  * @return Material 
	  * @throws
	  */
	
	public Material getMaterial(Long id){
	       // 实力化实体类
		   Material material=new Material();
	       
		   // 执行查询语句，获取基本信息
		   material=materialDao.get(id);
	        
	        return material;
	    }

	
	/**
	 * 生成物料系统编码
	 * 
	 * @param material
	 * @return
	 * @throws ParseException 
	 */
	public String createSystemCode(Material material) throws ParseException {
		if (material != null) {
			// 物料系统code
			String materialSystemCodeStr = "";
			// 组织实体
			OgnizationPEntity ognizationPEntity = new OgnizationPEntity();
			// 物料类别简称
			String materialCategoryStr = "XX";
			// 客户简称
			String customerSimpleNameStr = "XX";
			// 设置连翻号
			String sequenceCountStr = SequenceUtil.materialSystemCode(ResourcesUtil.getConfig("sequence_materialSystemCode")).getReturnCode();
			// 获取当前日期，格式'yyMM'。
			String thisDateStr = DateUtils.getDate("yyMM");
			// 物料成分简称
			String materialCompositionStr = "XXX";
			// 物料成分简称
			String repairStr = "XXXXXX";
			// 物料类别判断
			if (material.getMaterialTypeCd() != null && !"".equals(material.getMaterialTypeCd())) {
				// 获取物料类别简称
				materialCategoryStr = DictUtils.getDictEntity(material.getMaterialTypeCd()).getItemRealValue();
				if (materialCategoryStr == null || "".equals(materialCategoryStr)) {
					materialCategoryStr = "XX";
				}
			}
			// 客户判断
			if (material.getMaterialOgnizationId() != null && !"".equals(material.getMaterialOgnizationId())) {
				// 设置客户id
				ognizationPEntity.setOgnizationId(Long.parseLong(material.getMaterialOgnizationId()));
				// 获取客户信息数据
				ognizationPEntity = ognizationPEntityMapper.get(ognizationPEntity);
				if (ognizationPEntity != null) {
					//获取客户简称。
					customerSimpleNameStr = ognizationPEntity.getOgnizationSimpleName();
					// 若不为空，则获取固定长度数据。
					if (customerSimpleNameStr != null && !"".equals(customerSimpleNameStr)) {
						// 若简称长度大于2，截取两位长度。
						if (customerSimpleNameStr.length() > 2) {
							customerSimpleNameStr = customerSimpleNameStr.substring(0, 2);
						// 若简称长度小于2，补一位'X'。
						}else if (customerSimpleNameStr.length() < 2){
							customerSimpleNameStr = customerSimpleNameStr + "X";
						}
					// 若为空，赋'XX'。
					}else {
						customerSimpleNameStr = "XX";
					}
				}
			}
			// 物料成分判断
			if (material.getMaterialComposition()!= null && !"".equals(material.getMaterialComposition())) {
				// 获取物料成分简称
				materialCompositionStr = DesignoptionUtils.getDesignoptionEntity(material.getMaterialComposition().split("-")[1]).getRemarks();
				if (materialCompositionStr == null || "".equals(materialCompositionStr)) {
					materialCompositionStr = "XXX";
				}
			}
			// 拼接获取完整物料系统code
			materialSystemCodeStr = materialCategoryStr + customerSimpleNameStr + sequenceCountStr
									+ thisDateStr + materialCompositionStr + repairStr;
			// 返回数据
			return materialSystemCodeStr;
		}
		return "";
	}
	
	/**
	 * 
	 * @param code
	 * @return
	 */
	public boolean checkMaterialSystemCode(String code){
		
		return false;
	}

	
	/**
	 * @throws ParseException 
	  * @Title: saveMaterial
	  * @Description: 保存更新我的物料
	  * @param @param material
	  * @return void 
	  * @throws
	  */
	public void saveMaterial(Material material) throws ParseException {
		
		// 保存图片
		if(material.getUploadFile()!=null){
			
		    UploadFile uploadFile=material.getUploadFile();
        	
		    if(!CollectionsUtils.isEmpty(uploadFileDao.searchFileId(uploadFile))){
        		
        		// 返回查询结果id
		        material.setFileId(uploadFile.getFileId());
        	}else{
        	    uploadFile.preInsert();
        		// 若为null 执行保存操作
        	    uploadFile.setFileKbn(0);
        	    materialDao.saveUploadFile(uploadFile);
        		
        		material.setFileId(uploadFile.getFileId());
        		
        	}
		}
		// 设置物料系统code
		String materialSystemCode = createSystemCode(material);
		material.setMaterialSystemCode(materialSystemCode);
		// 判断新建 编辑
		// 若为null时执行插入操作（新建）
		if (material.getId()==null){
			material.preInsert();
			materialDao.insert(material);
			materialDao.insertVenderMaterial(material);
			materialDao.insertiInventoryMaterial(material);
			materialDao.insertBarCode(material);
			
		// 编辑
		}else{
			// 更新数据
			material.preUpdate();
			materialDao.update(material);
			Material materialVender = materialDao.findVenderMaterial(material);
			if(materialVender==null){
				materialDao.insertVenderMaterial(material);
			}else{
				materialDao.updateVenderMaterial(material);
			}
		}
		
		
	}

	/**
	  * @Title: deleteMaterial
	  * @Description: 删除我的物料
	  * @param @param material
	  * @return void 
	  * @throws
	  */

	public void deleteMaterial(Material material) {
		
		// 逻辑删除
		materialDao.delete(material);
	}
	
	/**
	  * @Title: getByName
	  * @Description: 查重
	  * @param @param materialNameCn
	  * @param @return
	  * @return Material 
	  * @throws
	  */
	
	public Material getByCode(String Code) {
		
		// 实例化实体类
		Material material = new Material();
		material.setMaterialCode(Code);
		
		// 执行查询返回查询结果
		return materialDao.getByCode(material);
	}
	
	/**
	 * @Title: findMaterialOgnizationList
	 * @Description: 来源机构一览
	 * @return List<Ognization>
	 */
	public List<Ognization> findMaterialOgnizationList(Material material) {
		material.setCurrentUser(material.getCurrentUser());
		return materialDao.findMaterialOgnizationList(material);
	}
	/**
     * @Title: selBrandList
     * @Description: 查询
     * @param @return
     * @return List<Brand>
     * @throws
     */
   public List<Brand> selBrandList(Material material) {

	   return materialDao.findBrandList();
   }
   
   
   /**
    * @Title: selBrandList
    * @Description: 查询
    * @param @return
    * @return List<Brand>
    * @throws
    */
  public List<Material> findButtonList() {

	   return materialDao.findButtonList();
  }
   
   /**
    * @Title: selBrandList
    * @Description: 查询
    * @param @return
    * @return List<Brand>
    * @throws
    */
  public List<Brand> selectBrandCnList(Material material) {
	   List<Brand> brandIDList = new ArrayList<>();
		// 查询供应厂对应的品牌个数
	   material = materialDao.selectBrankId(material);
		if (material !=null ) {
			String brandVar = material.getBrandIdStr();
			ArrayList alBrandTemp = new ArrayList();
			if (brandVar != null) {
				String [] barndTemp = brandVar.split(",");
				for (int i = 0; i < barndTemp.length; i++){
					alBrandTemp.add(barndTemp[i]);
				}
			}
			material.setBrandIDList(alBrandTemp);
			// 查询对应的品牌中文
			brandIDList = materialDao.selectBrankNameCn(material);
		}
		return brandIDList;

  }
   /**
    * @Title: selBrandList
    * @Description: 查询
    * @param @return
    * @return Ognization
    * @throws
    */
  public Ognization getMaterialOgnization(Material material){
	  Ognization ognization = materialDao.getMaterialOgnization(material);
	  List<Brand> brandList = materialDao.findBrandList();
	  if (ognization!=null) {
		  ognization.setSelBrandList(brandList);
	  }
	   return ognization;
  }
  
  /**
   * excel数据插入
   * @param styleList
   * @param orderList
   * @return
   */
 public void insertExcelData (List<Material> materialList) throws Exception {
	 Material material;  
	 BarCode barCode;
	 long userId = UserUtils.getUser().getId();
	 long updateId = UserUtils.getUser().getId();
     // 订单表插入
     if (materialList!=null) {
         int materialLen = materialList.size();
         for (int i = 0; i < materialLen; i++) {
        	 material = new Material();
        	 barCode = new BarCode();
             /**
              * 订单表插入
              */
             // 面料号
             material.setMaterialCode(materialList.get(i).getMaterialCode());             
             //物料类别
             material.setMaterialTypeCd(materialList.get(i).getMaterialTypeCd());
             // 面料名
             material.setMaterialNameCn(materialList.get(i).getMaterialNameCn());
             // 幅宽
             material.setMaterialSpec(materialList.get(i).getMaterialSpec());
             // 物料单位
             material.setMaterialUnitCd(materialList.get(i).getMaterialUnitCd());
             // 物料克数
             material.setMaterialGramWeight(materialList.get(i).getMaterialGramWeight());
             // 物料年份
             material.setMaterialYear(materialList.get(i).getMaterialYear());
             // 物料季节
             material.setMaterialSeason(materialList.get(i).getMaterialSeason());
             // 物料成分
             material.setMaterialComposition(materialList.get(i).getMaterialComposition());
             // 物料织法
             material.setMaterialKnitMethod(materialList.get(i).getMaterialKnitMethod());             
             // 面料颜色
             material.setMaterialColor(materialList.get(i).getMaterialColor());
             // 风格
             material.setMaterialStyle(materialList.get(i).getMaterialStyle());
             // 物料用途
             material.setMaterialUseType(materialList.get(i).getMaterialUseType());
             // 纱支/密度
             material.setMaterialThreadCount(materialList.get(i).getMaterialThreadCount());
             // 客户
             material.setOgnizationName(materialList.get(i).getOgnizationName());
             // 品牌
//             material.setBrandName(materialList.get(i).getBrandName());
             // 供应商
             material.setMaterialSupplierName(materialList.get(i).getMaterialSupplierName());
             // 物料级别
             material.setMaterialLevel(materialList.get(i).getMaterialLevel());
             // 产地
             material.setMaterialProduced(materialList.get(i).getMaterialProduced());
             // 面料使用类别
             material.setMaterialType(materialList.get(i).getMaterialType());
             // 客户
             material.setVenderId(materialList.get(i).getVenderId());
             // 条形码
             material.setMaterialBarCode(materialList.get(i).getMaterialBarCode());
             material.setUserId(userId);
             material.setUpdateId(updateId);  
             // 设置物料系统code
             String materialSystemCode = createSystemCode(material);
     		 material.setMaterialSystemCode(materialSystemCode);
             /**
              * 条码code表插入
              */
             barCode.setQrType("1");
             barCode.setQrBarCode(material.getMaterialBarCode());
             barCode.setQrName(materialList.get(i).getMaterialCode());
             barCode.setQrOther1(materialList.get(i).getMaterialSpec());
             barCode.setQrOther2(materialList.get(i).getMaterialComposition());
             barCode.setQrOther3(materialList.get(i).getMaterialColor());
             barCode.setQrOther4(materialList.get(i).getMaterialSupplierName());
             barCode.setQrOther5(materialList.get(i).getMaterialProduced());
             barCode.preInsert();
             // 原辅料信息管理表插入
             materialDao.inserMaterial(material);
             // 原辅料库存表插入
             materialDao.inserInventoryMat(material);
             // 客户原辅料关联表插入
             materialDao.inserMatAndOgn(material);
             // 条码code表插入
             materialDao.insertQRBarCode(barCode);
         }
     }
 }
	 /**
	  * @Title: selBrandList
	  * @Description: Excel查存
	  * @param @return
	  * @return boolean
	  * @throws
	  */
	public boolean findMaterialExcel(String code){
		Material material = materialDao.findMaterialExcel(code);
		if (material==null) {
			return true;
		}
		   return false;
	}

	 /**
	   * excel数据插入
	   * @param styleList
	   * @param orderList
	   * @return
	   */
	 public void insertData (List<Material> materialList){
		 List<Material> inList = new ArrayList<>();
		 List<Material> outList = new ArrayList<>();
		 List<Material> moveList = new ArrayList<>();
		 if (materialList!=null) {
			 String cusSimpName = materialDao.getCustomSimpleName(materialList.get(0).getCustomerIDStr());
			 for (int i = 0; i < materialList.size(); i++) {
				if ("1".equals(materialList.get(i).getInventoryInOutKbn())) {
					inList.add(materialList.get(i));
				}else if ("2".equals(materialList.get(i).getInventoryInOutKbn())) {
					outList.add(materialList.get(i));
				}else if ("9".equals(materialList.get(i).getInventoryInOutKbn())) {
					moveList.add(materialList.get(i));
				}
			 }
			 if ( inList != null && inList.size() > 0 ) {
				 InventoryRequest inventoryRequest = new InventoryRequest();
				 InventoryRequest inventoryRequest2 = new InventoryRequest();
				 inventoryRequest.setInventoryRequestNo("I."+cusSimpName+SequenceUtil.inventoryRequest().getReturnCode());
				 inventoryRequest.setStockBarCode(SequenceUtil.inventoryRequestbarcode().getReturnCode());
				 inventoryRequest.setInventoryCategory("INVENTORY_CATEGORY-CUSTOMER");
				 inventoryRequest.setVenderId(Long.parseLong(materialList.get(0).getCustomerIDStr().split(":")[0]));
				 inventoryRequest.setInventoryStatus(ResourcesUtil.getConfig("inventory_status_all"));
				 inventoryRequest.setInventoryInOutKbn(ResourcesUtil.getConfig("inventory_in_out_kbn_in"));
				 inventoryRequest.preInsert();
				 materialDao.insertInventoryRequest(inventoryRequest);
				 inventoryRequest2.setInventoryStatus(ResourcesUtil.getConfig("inventory_status_all"));
				 inventoryRequest2.setInventoryInOutKbn(ResourcesUtil.getConfig("inventory_in_out_kbn_in"));
				 inventoryRequest2.setInventoryCategory("INVENTORY_CATEGORY-CUSTOMER");
				 inventoryRequest2.setId(inventoryRequest.getId());
				 inventoryRequest2.preInsert();
				 for (int i = 0; i < inList.size(); i++) {
					 inventoryRequest2.setMaterialId(inList.get(i).getId());
					 inventoryRequest2.setRequestQuantity(new BigDecimal(inList.get(i).getInQuantity()));
					 inventoryRequest2.setWarehouseID(inList.get(i).getInWarehousePlace().split(":")[0]);
					 inventoryRequest2.setRackID(inList.get(i).getInRackPlace().split(":")[0]);
					 Long inventoryRequestDetailId = materialDao.findMaterialDetailExist(inList.get(i).getId(),inventoryRequest.getId());
					 if (inventoryRequestDetailId==null) {
						materialDao.insertInventoryRequestDetails(inventoryRequest2);
						materialDao.insertMaterialDetail(inventoryRequest2);
						materialDao.plusQuantity(inventoryRequest2);
					 }else {
						inventoryRequest2.setInventoryRequestDetailID(inventoryRequestDetailId);
						materialDao.updateInventoryRequestDetails(inventoryRequest2);
						materialDao.insertMaterialDetail(inventoryRequest2);
						materialDao.plusQuantity(inventoryRequest2);
					}
				 }
			 }
			 if ( outList != null && outList.size() > 0 ) {
				 InventoryRequestEx inventoryRequestEx = new InventoryRequestEx();
				 InventoryRequestEx inventoryRequestEx2 = new InventoryRequestEx();
				 inventoryRequestEx.setInventoryRequestNo("O."+cusSimpName+SequenceUtil.inventoryRequestEx().getReturnCode());
				 inventoryRequestEx.setStockBarCode(SequenceUtil.inventoryRequestbarcode().getReturnCode());
				 inventoryRequestEx.setInventoryCategory("OUTBOUND_CATEGORY-PRODUCE");
				 inventoryRequestEx.setInventoryStatus(ResourcesUtil.getConfig("outbound_status_all"));
				 inventoryRequestEx.setInventoryInOutKbn(ResourcesUtil.getConfig("inventory_in_out_kbn_out"));
				 inventoryRequestEx.preInsert();
				 materialDao.insertInventoryRequestEx(inventoryRequestEx);
				 inventoryRequestEx2.setInventoryStatus(ResourcesUtil.getConfig("inventory_status_all"));
				 inventoryRequestEx2.setInventoryInOutKbn(ResourcesUtil.getConfig("inventory_in_out_kbn_out"));
				 inventoryRequestEx2.setInventoryCategory("OUTBOUND_CATEGORY-PRODUCE");
				 inventoryRequestEx2.setId(inventoryRequestEx.getId());
				 inventoryRequestEx2.preInsert();
				 for (int i = 0; i < outList.size(); i++) {
					 inventoryRequestEx2.setMaterialId(outList.get(i).getId());
					 inventoryRequestEx2.setRequestQuantity(new BigDecimal(outList.get(i).getOutQuantity()));
					 inventoryRequestEx2.setWarehouseID(outList.get(i).getOutWarehousePlace().split(":")[0]);
					 inventoryRequestEx2.setRackID(outList.get(i).getOutRackPlace().split(":")[0]);
					 Long inventoryRequestExDetailId = materialDao.findMaterialExDetailExist(outList.get(i).getId(),inventoryRequestEx.getId());
					 if (inventoryRequestExDetailId==null) {
						materialDao.insertInventoryRequestExDetails(inventoryRequestEx2);
						materialDao.insertMaterialDetail(inventoryRequestEx2);
						materialDao.minusQuantity(inventoryRequestEx2);
					}else {
						inventoryRequestEx2.setInventoryRequestDetailID(inventoryRequestExDetailId);
						materialDao.updateInventoryRequestExDetails(inventoryRequestEx2);
						materialDao.insertMaterialDetail(inventoryRequestEx2);
						materialDao.minusQuantity(inventoryRequestEx2);
					}
				 }
			 }
			 if ( moveList != null && moveList.size() > 0 ) {
				 InventoryRequest inventoryRequest = new InventoryRequest();
				 InventoryRequest inventoryRequest2 = new InventoryRequest();
				 inventoryRequest.setInventoryRequestNo("I."+cusSimpName+SequenceUtil.inventoryRequest().getReturnCode());
				 inventoryRequest.setStockBarCode(SequenceUtil.inventoryRequestbarcode().getReturnCode());
				 inventoryRequest.setInventoryCategory("INVENTORY_CATEGORY-TRANSFER");
				 inventoryRequest.setVenderId(Long.parseLong(materialList.get(0).getCustomerIDStr().split(":")[0]));
				 inventoryRequest.setInventoryStatus(ResourcesUtil.getConfig("inventory_status_all"));
				 inventoryRequest.setInventoryInOutKbn(ResourcesUtil.getConfig("inventory_in_out_kbn_in"));
				 inventoryRequest.preInsert();
				 materialDao.insertInventoryRequest(inventoryRequest);
				 inventoryRequest2.setInventoryStatus(ResourcesUtil.getConfig("inventory_status_all"));
				 inventoryRequest2.setInventoryInOutKbn(ResourcesUtil.getConfig("inventory_in_out_kbn_in"));
				 inventoryRequest2.setInventoryCategory("INVENTORY_CATEGORY-TRANSFER");
				 inventoryRequest2.setId(inventoryRequest.getId());
				 inventoryRequest2.preInsert();
				 for (int i = 0; i < moveList.size(); i++) {
					 inventoryRequest2.setMaterialId(moveList.get(i).getId());
					 inventoryRequest2.setRequestQuantity(new BigDecimal(moveList.get(i).getInQuantity()));
					 inventoryRequest2.setWarehouseID(moveList.get(i).getInWarehousePlace().split(":")[0]);
					 inventoryRequest2.setRackID(moveList.get(i).getInRackPlace().split(":")[0]);
					 Long inventoryRequestDetailId = materialDao.findMaterialDetailExist(moveList.get(i).getId(),inventoryRequest.getId());
					 if (inventoryRequestDetailId==null) {
						materialDao.insertInventoryRequestDetails(inventoryRequest2);
						materialDao.insertMaterialDetail(inventoryRequest2);
					}else {
						inventoryRequest2.setInventoryRequestDetailID(inventoryRequestDetailId);
						materialDao.updateInventoryRequestDetails(inventoryRequest2);
						materialDao.insertMaterialDetail(inventoryRequest2);
					}
				 }
				 InventoryRequestEx inventoryRequestEx = new InventoryRequestEx();
				 InventoryRequestEx inventoryRequestEx2 = new InventoryRequestEx();
				 inventoryRequestEx.setInventoryRequestNo("O."+cusSimpName+SequenceUtil.inventoryRequestEx().getReturnCode());
				 inventoryRequestEx.setStockBarCode(SequenceUtil.inventoryRequestbarcode().getReturnCode());
				 inventoryRequestEx.setInventoryCategory("OUTBOUND_CATEGORY-TRANSFER");
				 inventoryRequestEx.setInventoryStatus(ResourcesUtil.getConfig("outbound_status_all"));
				 inventoryRequestEx.setInventoryInOutKbn(ResourcesUtil.getConfig("inventory_in_out_kbn_out"));
				 inventoryRequestEx.preInsert();
				 materialDao.insertInventoryRequestEx(inventoryRequestEx);
				 inventoryRequestEx2.setInventoryStatus(ResourcesUtil.getConfig("inventory_status_all"));
				 inventoryRequestEx2.setInventoryInOutKbn(ResourcesUtil.getConfig("inventory_in_out_kbn_out"));
				 inventoryRequestEx2.setInventoryCategory("OUTBOUND_CATEGORY-TRANSFER");
				 inventoryRequestEx2.setId(inventoryRequestEx.getId());
				 inventoryRequestEx2.preInsert();
				 for (int i = 0; i < moveList.size(); i++) {
					 inventoryRequestEx2.setMaterialId(moveList.get(i).getId());
					 inventoryRequestEx2.setRequestQuantity(new BigDecimal(moveList.get(i).getOutQuantity()));
					 inventoryRequestEx2.setWarehouseID(moveList.get(i).getOutWarehousePlace().split(":")[0]);
					 inventoryRequestEx2.setRackID(moveList.get(i).getOutRackPlace().split(":")[0]);
					 Long inventoryRequestExDetailId = materialDao.findMaterialExDetailExist(moveList.get(i).getId(),inventoryRequestEx.getId());
					 if (inventoryRequestExDetailId==null) {
						materialDao.insertInventoryRequestExDetails(inventoryRequestEx2);
						materialDao.insertMaterialDetail(inventoryRequestEx2);
					}else {
						inventoryRequestEx2.setInventoryRequestDetailID(inventoryRequestExDetailId);
						materialDao.updateInventoryRequestExDetails(inventoryRequestEx2);
						materialDao.insertMaterialDetail(inventoryRequestEx2);
					}
				 }
			 }
		 }
	 }
	 
	 /**
	   * 查存
	   * @param material
	   * @return
	   */
	 public Material findMaterialExist(Material material){
		 return materialDao.findMaterialExist(material);
	 }
	 
	 /**
	   * 查存
	   * @param material
	   * @return
	   */
	 public Double findMaterialQuntity(Material material){
		 return materialDao.findMaterialQuntity(material);
	 }
	 
	 /**
	   * 查存
	   * @param material
	   * @return
	   */
	 public Double findMaterialQuntityOut(Material material){
		 return materialDao.findMaterialQuntityOut(material);
	 }
	 
	 /**
		 * 获取仓库货架
		 * 
		 * @return
		 */
		public List<InventoryRequest> checkWarehouseRack(){
			return materialDao.checkWarehouseRack();
		}
		
		/**
		 * 获取客户品牌
		 * 
		 * @return
		 */
		public List<InventoryRequestEx> checkVenderBrand(){
			return materialDao.checkVenderBrand();
		}
		
		/**
		 * 面料ID获取出入库信息
		 * 
		 * @return
		 */
		public Material findInOutDetail(Material material){
			List<Material> inOutList = materialDao.findInOutDetail(material);
			List<Material> stockList = materialDao.findStockDetail(material);
			material = materialDao.getMaterialInfo(material);
			material.setMaterialList(inOutList);
			material.setStockList(stockList);
			return material;
		}
		
		/**
		 * 通过面料ID获取库存预警信息
		 */
		public InventoryWarning getInventoryWarningInfo(InventoryWarning inventoryWarning){
			return inventoryWarningDao.getInventoryWarningInfo(inventoryWarning);
		}
		
		/**
		 * 更新库存预警
		 */
		public void updateInventoryWarning(InventoryWarning inventoryWarning){
			inventoryWarningDao.update(inventoryWarning);
		}
		
		/**
		 * 更新库存预警
		 */
		public void insertInventoryWarning(InventoryWarning inventoryWarning){
			inventoryWarningDao.insert(inventoryWarning);
		}
		
		/**
		 * 保存备注（出入库原因）
		 * @param inventoryDetailID
		 * @param remarks
		 * @throws SQLException
		 */
		public void saveRemarks(String inventoryDetailID, String remarks) throws SQLException{
			materialDao.updateRemarks(inventoryDetailID, remarks);
		}
		
		
}
